package textapp.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import textapp.dao.model.Group;
import textapp.dao.model.User;

public class UserDao {
	
	public User findById(int userId) {
		EntityManager em = EMUtil.createEntityManager();
		User u = em.find(User.class, userId);
		em.close();
		return u;
	}
	
	public User updateNameStatus(User user, String name, int status) {
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		
		User u = em.find(User.class, user.getId());
		if (u != null) {
			u.setName(name);
			u.setStatus(status);
		}
		
		em.getTransaction().commit();
		em.close();
		return user;
	}
	
	public List<User> findUsersByIds(List<Integer> idList) {
		List<User> ret = new ArrayList<User>();
		EntityManager em = EMUtil.createEntityManager();
		for (Integer id: idList) {
			User u = em.find(User.class, id.intValue());
			if (u != null) {
				ret.add(u);
			}
		}
		em.close();
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public User findByIdentity(String identity) {
		EntityManager em = EMUtil.createEntityManager();
		Query q = em.createNamedQuery("User.getByIdentity");
		q.setParameter("identity", identity);
		List<User> result = q.getResultList();
		if (result.size() > 0) {
			return result.get(0);
		}else {
			return null;
		}
	}
	
	public User create(String name, String identity, int status) {
		User u = new User();
		u.setName(name);
		u.setIdentity(identity);
		u.setStatus(status);
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		em.persist(u);
		em.getTransaction().commit();
		em.close();
		return u;
	}
	
	public List<User> getUsersFromIdentities(List<String> identityList) {
		List<User> users = new ArrayList<User>();
		EntityManager em = EMUtil.createEntityManager();
		for (String s : identityList) {
			Query q = em.createNamedQuery("User.getByIdentity");
			q.setParameter("identity", s);
			@SuppressWarnings("unchecked")
			List<User> result = q.getResultList();
			if (result.size() > 0) {
				users.add(result.get(0));
			}
		}
		em.close();
		return users;
	}
	
	public boolean deleteUser(int userId) {
		boolean result = false;
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		User user = em.find(User.class, userId);
		try {
			em.remove(user);
			result = true;
		} catch(Throwable t) {
			t.printStackTrace();
		}
		em.getTransaction().commit();
		em.close();
		return result;
	}
	
	public boolean saveRegistrationId(int userId, String regisId) {
		boolean result = false;
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		
		User u = em.find(User.class, userId);
		if (u == null) {
			em.close();
			return false;
		}
		u.setPushIdentity(regisId);
		em.getTransaction().commit();
		em.close();
		result = true;
		return result;
	}
	
}
